package development.hdk.database;



import java.util.LinkedList;

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;
import development.hdk.node.Node;
import development.hdk.mindmapfornandroid.Attributes;
import development.hdk.tree.Tree;

public class BackEnd {
	  Context context;
	  private SQLiteDatabase db;
	  private DatabaseHelper dbHelper;
	  public BackEnd(Context paramContext)
	  {
	    this.dbHelper = new DatabaseHelper(paramContext, "mindmap", null, 1);
	    this.context = paramContext;
	  }
	  public void open() throws SQLException
	  {
	    try
	    {
	      this.db = this.dbHelper.getWritableDatabase();
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void close()
	  {
	    try
	    {
	      this.db.close();
	      this.dbHelper.close();
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void commitTransaction()
	  {
	    try
	    {
	      this.db.setTransactionSuccessful();
	      this.db.endTransaction();
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public int createNode(int paramInt, float paramFloat1, float paramFloat2, Node paramNode, String paramString, Attributes paramAttributes)
	  {
	    try
	    {
	      int i = getNextID("nodes");
	      int j = 0;
	      if (paramAttributes.getShowIcon())
	        j = 1;
	      String str1 = DatabaseUtils.sqlEscapeString(paramString);
	      String str2 = "INSERT INTO nodes VALUES (" + i + ", " + str1 + ", " + paramFloat1 + ", " + paramFloat2 + ", " + paramNode.getID() + ", " + paramAttributes.getBackgroundColor() + ", " + paramAttributes.getBorderColor() + ", '" + paramAttributes.getIconDrawable() + "', " + j + ", " + paramAttributes.getShape() + ", " + paramInt + ");";
	      this.db.execSQL(str2);
	      return i;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	      {
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	        int i = 0;
	      }
	    }
	  }
	  public int createTree(String paramString1, Attributes paramAttributes, String paramString2, int paramInt)
	  {
	    try
	    {
	      int i = getNextID("mindmap");
	      int j = 0;
	      if (paramAttributes.getShowIcon())
	        j = 1;
	      String str1 = DatabaseUtils.sqlEscapeString(paramString1);
	      String str2 = "INSERT INTO mindmap VALUES (" + i + ", " + str1 + ", '" + paramString2 + "', " + paramInt + ");";
	      this.db.execSQL(str2);
	      int k = getNextID("nodes");
	      String str3 = "INSERT INTO nodes VALUES (" + k + ", " + str1 + ", " + 0 + ", " + 0 + ", " + -1 + ", " + paramAttributes.getBackgroundColor() + ", " + paramAttributes.getBorderColor() + ", '" + paramAttributes.getIconDrawable() + "', " + j + ", " + paramAttributes.getShape() + ", " + i + ");";
	      this.db.execSQL(str3);
	      return i;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	      {
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	        int i = 0;
	      }
	    }
	  }
	  public void deleteNode(Node paramNode)
	  {
	    try
	    {
	      String str = "DELETE FROM nodes WHERE _id=" + paramNode.getID() + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void deleteTree(int paramInt)
	  {
	    try
	    {
	      String str1 = "DELETE FROM nodes WHERE treeID=" + paramInt + ";";
	      this.db.execSQL(str1);
	      String str2 = "DELETE FROM mindmap WHERE _id=" + paramInt + ";";
	      this.db.execSQL(str2);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public int getNextID(String paramString)
	  {
	    int i = -1;
	    int j;
	    try
	    {
	      String str = "SELECT _id FROM " + paramString + ";";
	      Cursor localCursor = this.db.rawQuery(str, null);
	      localCursor.moveToFirst();
	      while (true)
	      {
	        if (localCursor.isAfterLast())
	        {
	          localCursor.close();
	          j = i + 1;
	          break;
	        }
	        int k = localCursor.getInt(0);
	        if (k > i)
	          i = k;
	        localCursor.moveToNext();
	      }
	    }
	    catch (Exception localException)
	    {
	      Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	      j = 0;
	    }
	    return j;
	  }
	  public Cursor getNodes(int paramInt)
	  {
		  Object localObject = null;
		    try
		    {
		      String str = "SELECT * FROM nodes WHERE treeID=" + paramInt + ";";
		      Cursor localCursor = this.db.rawQuery(str, null);
		      localObject = localCursor;
		      return (Cursor)localObject;
		    }
		    catch (Exception localException)
		    {
		      while (true)
		        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
		    }
	  }
	  public LinkedList<Tree> getAllTrees()
	  {
	    LinkedList localLinkedList;
	    try
	    {
	      localLinkedList = new LinkedList();
	      Cursor localCursor = this.db.rawQuery("SELECT * FROM mindmap;", null);
	      localCursor.moveToFirst();
	      while (true)
	      {
	        if (localCursor.isAfterLast())
	        {
	          localCursor.close();
	          break;
	        }
	        Tree localTree = new Tree(localCursor.getInt(0), localCursor.getString(1), localCursor.getString(2), localCursor.getInt(3));
	        localCursor.moveToNext();
	        localLinkedList.add(localTree);
	      }
	    }
	    catch (Exception localException)
	    {
	      Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	      localLinkedList = null;
	    }
	    return localLinkedList;
	  }
	  public boolean inTransaction()//Co fix
	  {
	    try
	    {
	      boolean bool = this.db.inTransaction();
	      return bool;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void rollbackTransaction()
	  {
	    try
	    {
	      this.db.endTransaction();
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void setStyle(int paramInt1, int paramInt2, int paramInt3, int paramInt4)
	  {
	    try
	    {
	      String str = "UPDATE nodes SET backgroundColor=" + paramInt3 + ", " + "borderColor=" + paramInt4 + ", " + "shape=" + paramInt2 + " WHERE treeID=" + paramInt1 + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void startTransaction()
	  {
	    try
	    {
	      this.db.beginTransaction();
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateNodeBackground(Node paramNode)
	  {
	    try
	    {
	      String str = "UPDATE nodes SET backgroundColor=" + paramNode.attributes.getBackgroundColor() + " WHERE " + "_id=" + paramNode.getID() + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateNodeBorder(Node paramNode)
	  {
	    try
	    {
	      String str = "UPDATE nodes SET borderColor=" + paramNode.attributes.getBorderColor() + " WHERE " + "_id=" + paramNode.getID() + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateNodeIcon(Node paramNode)
	  {
	    int i = 0;
	    try
	    {
	      if (paramNode.attributes.getShowIcon())
	        i = 1;
	      String str = "UPDATE nodes SET iconDrawable='" + paramNode.attributes.getIconDrawable() + "', " + "showIcon=" + i + " WHERE " + "_id=" + paramNode.getID() + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateNodeLocation(Node paramNode)
	  {
	    try
	    {
	      String str = "UPDATE nodes SET locationX=" + paramNode.getLocationX() + ", " + "locationY=" + paramNode.getLocationY() + " WHERE " + "_id=" + paramNode.getID() + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateNodeShape(Node paramNode)
	  {
	    try
	    {
	      String str = "UPDATE nodes SET shape=" + paramNode.attributes.getShape() + " WHERE " + "_id=" + paramNode.getID() + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateNodeText(Node paramNode)
	  {
	    try
	    {
	      String str1 = DatabaseUtils.sqlEscapeString(paramNode.getText());
	      String str2 = "UPDATE nodes SET text=" + str1 + " WHERE " + "_id=" + paramNode.getID() + ";";
	      this.db.execSQL(str2);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateSurfaceColor(int paramInt1, int paramInt2)
	  {
	    try
	    {
	      String str = "UPDATE mindmap SET surfaceColor=" + paramInt2 + " WHERE " + "_id=" + paramInt1 + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateTreeIcon(int paramInt, String paramString)
	  {
	    try
	    {
	      String str = "UPDATE mindmap SET icon='" + paramString + "' WHERE " + "_id=" + paramInt + ";";
	      this.db.execSQL(str);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
	  public void updateTreeName(int paramInt, String paramString)
	  {
	    try
	    {
	      String str1 = DatabaseUtils.sqlEscapeString(paramString);
	      String str2 = "UPDATE mindmap SET treeName=" + str1 + " WHERE " + "_id=" + paramInt + ";";
	      this.db.execSQL(str2);
	      return;
	    }
	    catch (Exception localException)
	    {
	      while (true)
	        Toast.makeText(this.context, "Database error: " + localException.getMessage(), 1).show();
	    }
	  }
}
